package cst.a112gxl.TeacherPlatform112gxl.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cst.a112gxl.TeacherPlatform112gxl.R;
import cst.a112gxl.TeacherPlatform112gxl.listener.AmicoolListener;
import cst.a112gxl.TeacherPlatform112gxl.model.Resmodel;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText username;
    private EditText password;
    private EditText rpassword;
    private EditText tel;
    private Button bregister;
    private AmicoolListener<String> listener;
    private Resmodel resmodel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);
        init();
        bregister.setOnClickListener(this);
        listener = new AmicoolListener<String>() {
            @Override
            public void onResponse(String list) {
                if (list.equals("1")){
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFail(String msg) {
                Toast.makeText(RegisterActivity.this,msg, Toast.LENGTH_SHORT).show();
            }
        };

    }

    private void init() {
        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);
        rpassword = (EditText)findViewById(R.id.rpassword);
        tel = (EditText)findViewById(R.id.tel);
        bregister = (Button) findViewById(R.id.bregister);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bregister:
                String pass = password.getText().toString();
                String rpass = rpassword.getText().toString();
                if (pass.equals(rpass)){
                    resmodel = new Resmodel("string");
                    resmodel.reg(username.getText().toString(),pass,tel.getText().toString(),listener);
                    break;
                }else {
                    Toast.makeText(this, "两次密码不同", Toast.LENGTH_SHORT).show();
                }
        }
    }
}
